package hxg.file;

import java.io.File;

/**
 * 运用递归循环所有文件
 * @author HeXiangGuang
 * @create 2021-06-29 16:34
 */
public class ListDemo {
    public static void main(String[] args) {
//        listDir(new File("d:\\myfiles"));
        deleteDir(new File("d:\\myfiles"));
    }
    //递归查看所有文件
    public static void listDir(File dir){
        File[] files = dir.listFiles();
        System.out.println(dir.getAbsolutePath());
        if (files !=null && files.length >0){
            for (File file : files) {
                if (file.isDirectory()){
                    listDir(file);
                }else {
                    System.out.println(file.getAbsolutePath());
                }
            }
        }
    }

    //递归删除所有文件

    public static void deleteDir(File dir){
        File[] files = dir.listFiles();
        System.out.println(dir.getAbsolutePath());
        if (files !=null && files.length >0){
            for (File file : files) {
                if (file.isDirectory()){
                    deleteDir(file);
                }else {
                    System.out.println(file.getAbsolutePath()+"被删除"+file.delete());
                }
            }
        }
        System.out.println(dir.getAbsolutePath()+"被删除"+dir.delete());
    }
}
